import { Entity } from "oasis-engine";

interface Options {
  targetValue: number;
  deltaValue: number;
}
export module Utils {
  export function translateY(
    entity: Entity,
    deltaValue: number,
    targetValue: number
  ) {
    const position = entity.transform.position;
    position.y = updateValue(position.y, deltaValue, targetValue);
    entity.transform.position = entity.transform.position;
    return position.y;
  }

  export function scaleX(
    entity: Entity,
    deltaValue: number,
    targetValue: number
  ) {
    const scale = entity.transform.scale;
    scale.x = updateValue(scale.x, deltaValue, targetValue);
    entity.transform.scale = entity.transform.scale;
    return scale.x;
  }

  export function scaleY(
    entity: Entity,
    deltaValue: number,
    targetValue: number
  ) {
    const scale = entity.transform.scale;
    scale.y = updateValue(scale.y, deltaValue, targetValue);
    entity.transform.scale = entity.transform.scale;
    return scale.y;
  }

  export function scaleZ(
    entity: Entity,
    deltaValue: number,
    targetValue: number
  ) {
    const scale = entity.transform.scale;
    scale.z = updateValue(scale.z, deltaValue, targetValue);
    entity.transform.scale = entity.transform.scale;
    return scale.z;
  }

  export function wait(ms: number) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(undefined);
      }, ms);
    });
  }

  function updateValue(
    currentValue: number,
    deltaValue: number,
    targetValue: number
  ): number {
    currentValue = currentValue + deltaValue;
    const func = deltaValue > 0 ? Math.min : Math.max;
    return func(currentValue, targetValue);
  }
}
